package com.example.foodwebbackend.service;

import com.example.foodwebbackend.entity.Admin;
import com.example.foodwebbackend.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 管理员登录验证
     */
    public Admin login(String adminUsername, String adminPassword) {
        Admin admin = adminMapper.findByUsername(adminUsername);
        if (admin == null) {
            throw new RuntimeException("管理员不存在");
        }

        if (!adminPassword.equals(admin.getAdminPassword())) {
            throw new RuntimeException("密码错误");
        }

        if (admin.getIsActive() == 0) {
            throw new RuntimeException("管理员账号已被禁用");
        }

        // 更新最后登录时间
        adminMapper.updateLastLoginTime(admin.getAdminId());

        return admin;
    }

    /**
     * 根据用户名查找管理员
     */
    public Admin findByUsername(String adminUsername) {
        return adminMapper.findByUsername(adminUsername);
    }

    /**
     * 根据管理员ID查找管理员
     */
    public Admin findById(Integer adminId) {
        return adminMapper.findById(adminId);
    }

    /**
     * 创建管理员
     */
    public Admin createAdmin(String adminUsername, String adminPassword, String adminName, String adminRole) {
        // 检查用户名是否已存在
        if (adminMapper.existsByUsername(adminUsername)) {
            throw new RuntimeException("管理员用户名已存在");
        }

        // 创建新管理员
        Admin admin = new Admin();
        admin.setAdminUsername(adminUsername);
        admin.setAdminPassword(adminPassword);
        admin.setAdminName(adminName);
        admin.setAdminRole(adminRole);
        admin.setAvatarPath("/businessmen.png");
        admin.setRegisterTime(LocalDateTime.now());
        admin.setIsActive(1); // 默认激活

        adminMapper.insert(admin);
        return admin;
    }

    /**
     * 更新管理员信息
     */
    public Admin updateAdmin(Admin admin) {
        if (admin.getAdminPassword() != null && !admin.getAdminPassword().isEmpty()) {
            admin.setAdminPassword(admin.getAdminPassword());
        }
        adminMapper.update(admin);
        return adminMapper.findById(admin.getAdminId());
    }

    /**
     * 删除管理员
     */
    public void deleteAdmin(Integer adminId) {
        adminMapper.deleteById(adminId);
    }

    /**
     * 获取所有管理员
     */
    public List<Admin> getAllAdmins() {
        return adminMapper.findAll();
    }

    /**
     * 根据激活状态获取管理员
     */
    public List<Admin> getAdminsByActiveStatus(Integer isActive) {
        return adminMapper.findByActiveStatus(isActive);
    }

    /**
     * 更新激活状态
     */
    public void updateActiveStatus(Integer adminId, Integer isActive) {
        adminMapper.updateActiveStatus(adminId, isActive);
    }

    /**
     * 检查管理员用户名是否存在
     */
    public boolean existsByUsername(String adminUsername) {
        return adminMapper.existsByUsername(adminUsername);
    }

    /**
     * 统计管理员总数
     */
    public int countAdmins() {
        return adminMapper.countAdmins();
    }
} 